package cn.tedu.egweibo1.controller;

import cn.tedu.egweibo1.mapper.CommentMapper;
import cn.tedu.egweibo1.pojo.dto.CommentDTO;
import cn.tedu.egweibo1.pojo.entity.Comment;
import cn.tedu.egweibo1.pojo.vo.CommentVO;
import cn.tedu.egweibo1.pojo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/comment/")
public class CommentController {

    @Autowired
    private CommentMapper commentMapper;

    @PostMapping("insert")
    public int insertComment(@RequestBody CommentDTO commentDTO, HttpSession session){
        /**
         * 插入评论：
         * 1.确认登录状态
         * 2.根据微博id
         * 3.根据评论用户id
         * 4.插入comment数据
         */

        UserVO userVO = (UserVO) session.getAttribute("user");
        if(userVO == null){
            return 2;
        } //未登录

        System.out.println(commentDTO);
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO,comment);
        comment.setUserId(userVO.getId());
        comment.setCreated(new Date());

        commentMapper.insertComment(comment);
        return 1;

    }

    @GetMapping("selectByWeiboId")
    public List<CommentVO> selectByWeiboId(Integer id){

        /**
         * 获取一则微博的所有评论
         * 1.获得微博id
         * 2.获取评论id,内容等
         */

        return commentMapper.selectByWeiboId(id);

    }
}
